﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4Shop.Models;

namespace MVC4Shop.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            ShoppingCartModel carts = new ShoppingCartModel();
            if (Session["carts"] != null)
                carts = (ShoppingCartModel)Session["carts"];
            return View(carts.GetProduct());
        }

        public JsonResult AddToCart(string product_id)
        {
            ShoppingCartModel carts;
            if (Session["carts"] == null)
            {
                carts = new ShoppingCartModel();
            }
            else
            {
                carts = (ShoppingCartModel)Session["carts"];
            }

            CartModel product = new CartModel();
            product.product_id = product_id;
            //product.price = price;
            //product.quantity = quantity;

            carts.AddToCart(product_id, product);
            Session["carts"] = carts;

            return Json(new { kq = true }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Delete(string product_id)
        {
            ShoppingCartModel carts;
            if (Session["carts"] == null)
            {
                carts = new ShoppingCartModel();
            }
            else
            {
                carts = (ShoppingCartModel)Session["carts"];
                carts.RemoveFromCart(product_id);
            }

            Session["carts"] = carts;
            return Json(new { kq = true }, JsonRequestBehavior.AllowGet);
        }

    }
}
